package com.zdsx.utils;

import java.util.Random;

public class RandomUtil {

    public static String generateRandomString(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder(length);
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            char randomChar = characters.charAt(index);
            sb.append(randomChar);
        }

        return sb.toString();
    }

    // 生成指定长度的验证码的方法
    public static String generateVerificationCode(int codeLength) {
        // 定义验证码字符集
        String codeChars = "0123456789";
        // 使用StringBuilder来拼接验证码
        StringBuilder verificationCode = new StringBuilder();
        // 创建Random对象
        Random random = new Random();
        // 循环生成指定长度的验证码
        for (int i = 0; i < codeLength; i++) {
            // 从字符集中随机选择一个字符
            char randomChar = codeChars.charAt(random.nextInt(codeChars.length()));
            // 将选定的字符追加到验证码中
            verificationCode.append(randomChar);
        }

        // 返回生成的验证码字符串
        return verificationCode.toString();
    }

}
